package io.rong.imkit.utils

import android.content.Context
import android.location.Address
import android.location.Geocoder
import android.location.LocationManager
import android.util.Log
import java.util.Locale
import java.util.concurrent.TimeUnit

class LocationUtils {
    fun getCity(context: Context, lat:Double, lng:Double):String{
        try {
            val geocoder = Geocoder(context, Locale.getDefault())
            val addresses: List<Address>? = geocoder.getFromLocation(lat, lng, 10)
            if (!addresses.isNullOrEmpty()) {
                val address: Address = addresses[0]
                Log.e("~~~", "initView: $address")
                if (address.subAdminArea != null) {
                    return address.subAdminArea
                } else {
                    if (address.locality != null) {
                        return address.locality.replace("Shi", "", true)
                    }
                }
                return ""
            } else {
                return ""
            }
        }catch (e:Exception){
            Log.e("~~~", "getCity: ${e.message.toString()}" )
            return ""
        }
        return ""
    }
}